package org.black.phoneUtility.sms.reply.app.fragment;

import org.black.phoneUtility.R;
import org.black.phoneUtility.sms.reply.app.service.SmsReplyNotificationIntentService;
import org.black.phoneUtility.sms.reply.provider.SmsReplyContract;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.util.Log;
import android.view.MenuItem;

public class SmsReplyPreferenceFragment extends PreferenceFragment implements
		SharedPreferences.OnSharedPreferenceChangeListener {
	private static final String LOG_TAG = SmsReplyPreferenceFragment.class
			.getName();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		addPreferencesFromResource(R.xml.sms_reply_perference);

		Log.i(LOG_TAG, "Load R.xml.sms_reply_perference.");

		getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
	}

	@Override
	public void onResume() {
		super.onResume();
		getPreferenceScreen().getSharedPreferences()
				.registerOnSharedPreferenceChangeListener(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		getPreferenceScreen().getSharedPreferences()
				.unregisterOnSharedPreferenceChangeListener(this);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			getActivity().onBackPressed();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		if (key.equals(getString(R.string.preference_sms_reply_enable_avoid_excessive_reply)) == true) {
			boolean enableAvoidExcessiveReply = sharedPreferences.getBoolean(
					key, true);
			ContentValues enableAvoidExcessiveReplyContentValues = new ContentValues();
			enableAvoidExcessiveReplyContentValues
					.put(SmsReplyContract.SmsReplyGlobalSettings.ENABLE_AVOID_EXCESSIVE_REPLY,
							true == enableAvoidExcessiveReply ? 1 : 0);
			getActivity()
					.getContentResolver()
					.update(ContentUris.withAppendedId(
							SmsReplyContract.SmsReplyGlobalSettings.CONTENT_URI,
							1l), enableAvoidExcessiveReplyContentValues, null,
							null);
		} else if (key
				.equals(getString(R.string.preference_sms_reply_show_notification)) == true) {
			boolean showOnNotification = sharedPreferences
					.getBoolean(key, true);
			ContentValues enableAvoidExcessiveReplyContentValues = new ContentValues();
			enableAvoidExcessiveReplyContentValues
					.put(SmsReplyContract.SmsReplyGlobalSettings.SHOULD_NOTIFICATION_DISPLAY_IN_NOTIFICATION_BAR,
							true == showOnNotification ? 1 : 0);
			getActivity()
					.getContentResolver()
					.update(ContentUris.withAppendedId(
							SmsReplyContract.SmsReplyGlobalSettings.CONTENT_URI,
							1l), enableAvoidExcessiveReplyContentValues, null,
							null);
			Intent updateNotificationServiceIntent = new Intent(getActivity(),
					SmsReplyNotificationIntentService.class);
			Bundle arguments = new Bundle();
			arguments
					.putInt(SmsReplyNotificationIntentService.ARGUMENTS_ACTION_TYPE,
							SmsReplyNotificationIntentService.ACTION_TYPE_SMS_REPLY_RECHECK);
			updateNotificationServiceIntent.putExtras(arguments);
			getActivity().startService(updateNotificationServiceIntent);
			Log.i(LOG_TAG, "Resend SmsReplyNotificationIntentService");
		} else if (key
				.equals(getString(R.string.preference_sms_reply_do_not_send_reply_message_on_roaming)) == true) {
			boolean doNotSendOnRoaming = sharedPreferences
					.getBoolean(key, true);
			ContentValues doNotSendOnRoamingContentValues = new ContentValues();
			doNotSendOnRoamingContentValues
					.put(SmsReplyContract.SmsReplyGlobalSettings.DO_NOT_SEND_REPLY_MESSAGE_ON_ROAMING,
							true == doNotSendOnRoaming ? 1 : 0);
			getActivity()
					.getContentResolver()
					.update(ContentUris.withAppendedId(
							SmsReplyContract.SmsReplyGlobalSettings.CONTENT_URI,
							1l), doNotSendOnRoamingContentValues, null, null);

		}

	}
}
